﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Media.Imaging;
using ZXing;
using ZXing.Common;
using ZXing.QrCode;
using ZXing.QrCode.Internal;

namespace LpbPrj.Client.Tools.Helper
{
    public class QrCodeHelper
    {
        /// <summary>
        /// 生成一维条形码
        /// </summary>
        /// <param name="text">内容</param>
        /// <param name="width">宽度</param>
        /// <param name="height">高度</param>
        /// <returns>Bitmap对象</returns>
        public static BitmapImage GenerateBarCode(string text, int width, int height)
        {
            Bitmap map = GenerateBarCodeBmp(text, width, height);
            return ImageShareHelper.BitmapToBitmapImage(map);
        }
        public static Bitmap GenerateBarCodeBmp(string text, int width, int height)
        {
            BarcodeWriter<Bitmap> writer = new BarcodeWriter<Bitmap>();
            //使用ITF 格式，不能被现在常用的支付宝、微信扫出来
            //如果想生成可识别的可以使用 CODE_128 格式
            //writer.Format = BarcodeFormat.ITF;
            writer.Format = BarcodeFormat.CODE_128;
            EncodingOptions options = new EncodingOptions()
            {
                Width = width,
                Height = height,
                Margin = 2,
                PureBarcode = true,
            };
            writer.Options = options;

            Bitmap map = writer.Write(text);
            return map;
        }

        /// <summary>
        /// 生成二维码
        /// </summary>
        /// <param name="text">内容</param>
        /// <param name="width">宽度</param>
        /// <param name="height">高度</param>
        /// <returns>Bitmap对象</returns>
        public static BitmapImage GenerateQRCode(string text, int width, int height)
        {
            BarcodeWriter<Bitmap> writer = new BarcodeWriter<Bitmap>();
            writer.Format = BarcodeFormat.QR_CODE;
            QrCodeEncodingOptions options = new QrCodeEncodingOptions()
            {
                DisableECI = true,//设置内容编码
                CharacterSet = "UTF-8",  //设置二维码的宽度和高度
                Width = width,
                Height = height,
                Margin = 1//设置二维码的边距,单位不是固定像素
            };
            writer.Options = options;
            Bitmap map = writer.Write(text);
            return ImageShareHelper.BitmapToBitmapImage(map);
        }

        /// <summary>
        /// 生成带Logo的二维码
        /// </summary>
        /// <param name="text">内容</param>
        /// <param name="width">宽度</param>
        /// <param name="height">高度</param>
        /// <param name="logo">Logo</param>
        /// <returns>Bitmap对象</returns>
        public static BitmapImage GenerateQRCodeWithLogo(string text, int width, int height, Bitmap logo)
        {
            //构造二维码写码器
            MultiFormatWriter writer = new MultiFormatWriter();
            Dictionary<EncodeHintType, object> hint = new Dictionary<EncodeHintType, object>();
            hint.Add(EncodeHintType.CHARACTER_SET, "UTF-8");
            hint.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
            //生成二维码 
            BitMatrix bm = writer.encode(text, BarcodeFormat.QR_CODE, width, height, hint);
            BarcodeWriter<Bitmap> barcodeWriter = new BarcodeWriter<Bitmap>();
            Bitmap map = barcodeWriter.Write(bm);
            //获取二维码实际尺寸
            int[] rectangle = bm.getEnclosingRectangle();
            //计算插入图片的大小和位置
            int middleW = Math.Min((int)(rectangle[2] / 3.5), logo.Width);
            int middleH = Math.Min((int)(rectangle[3] / 3.5), logo.Height);
            int middleL = (map.Width - middleW) / 2;
            int middleT = (map.Height - middleH) / 2;
            Bitmap bmpimg = new Bitmap(map.Width, map.Height, PixelFormat.Format32bppArgb);
            using (Graphics g = Graphics.FromImage(bmpimg))
            {
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                g.DrawImage(map, 0, 0, width, height);
            }
            //将二维码插入图片
            Graphics myGraphic = Graphics.FromImage(bmpimg);
            //白底
            myGraphic.FillRectangle(Brushes.White, middleL, middleT, middleW, middleH);
            myGraphic.DrawImage(logo, middleL, middleT, middleW, middleH);
            return ImageShareHelper.BitmapToBitmapImage(bmpimg);
        }

    }
}
